<?php
/*
 * author: daudm <dinhmanhdau@gmail.com>
 * */
class Comic_Model extends TinyMVC_Model {

  private $DB;
  private $memcache;
  private $timeout;

  function __construct() {
    parent::__construct();
   
    $this->DB = 'my_database';
    #$this->memcache = new MemCache();
    // $this->memcache->connect('localhost', 11211) or die ('could not connect memcache');
    $this->timeout = 600; // 10 minute
    $this->timeout2 = 7200; // 120 minute
  }

  function getStory() {
      $data = array();
      $db = $this->db->getDb('story');
      $collection = $db->comics;
      $rows = $collection->find()->limit(10);
      foreach ($rows as $row) {
        $data[] = array('name' => $row['name'], 'id' => (string)$row['_id']);
      }
      return $data;
  }

  function getStory2() {
    $key = 'pav2_ComicGetListCat';
    $result = $this->memcache->get($key);
    if (!$result) {
      $data = array();
      $db = $this->db->getDb('gallery');
      $collection = $db->category;
      $rows = $collection->find()->sort(array('order' => 1));
      foreach ($rows as $row) {
        $data[] = array('name' => $row['name'], 'id' => (string)$row['_id']);
      }
      if (count($data) > 0) {
         $this->memcache->set($key, $data, false, $this->timeout2);
      }
      return $data;
    } else {
      return $result;
    }
  }

} // end class
?>
